package com.xvdong.audioplayer.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.xvdong.audioplayer.R;
import com.xvdong.audioplayer.databinding.ItemLyricsListBinding;
import com.xvdong.audioplayer.interfaces.OnDataListener;
import com.xvdong.audioplayer.model.WYAudio;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by xvDong on 2024/3/27.
 */

public class LyricsListAdapter extends RecyclerView.Adapter<LyricsListAdapter.ViewHolder> {

    private List<WYAudio.ResultBean.SongsBean> mSongsBeans;
    private OnDataListener<Long> mListener;
    private Context mContext;

    public LyricsListAdapter(@NonNull Context context, List<WYAudio.ResultBean.SongsBean> songs, OnDataListener<Long> listener) {
        mSongsBeans = songs;
        mListener = listener;
        mContext = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemLyricsListBinding binding =
                DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_lyrics_list, parent, false);
        return new LyricsListAdapter.ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        WYAudio.ResultBean.SongsBean bean = mSongsBeans.get(position);
        holder.mBinding.llRoot.setOnClickListener(v -> {
            if (mListener != null){
                mListener.onDataListener(bean.getId());
            }
        });
        holder.bind(bean);
    }

    @Override
    public int getItemCount() {
        return mSongsBeans.size();

    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ItemLyricsListBinding mBinding;

        public ViewHolder(ItemLyricsListBinding binding) {
            super(binding.getRoot());
            mBinding = binding;
        }
        public void bind(WYAudio.ResultBean.SongsBean bean) {
            mBinding.setBean(bean);
        }
    }
}
